/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Encrypt.java
 *
 * Created on Sep 30, 2010, 4:10:46 PM
 */
package session04;

import com.sun.org.apache.xml.internal.security.utils.Base64;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

/**
 *
 * @author cibervn
 */
public class Encrypt_1 extends javax.swing.JPanel {

    //Cipher Object to encrypt or decrypt
    private Cipher cipher = null;
    private SecretKey secretKey = null;

    /** Creates new form Encrypt */
    public Encrypt_1() {
        initComponents();
        myInit();
    }

    private void myInit() {
        try {
            cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            secretKey = KeyGenerator.getInstance("DES").generateKey();
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchPaddingException ex) {
            Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    //@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnPlain = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtPlain = new javax.swing.JTextArea();
        btnEncrypt = new javax.swing.JButton();
        pnEncrypted = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtEncrypted = new javax.swing.JTextArea();
        btnDecrypt = new javax.swing.JButton();
        pnPlainResult = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        txtResult = new javax.swing.JTextArea();

        setLayout(new java.awt.GridLayout(3, 1));

        pnPlain.setBorder(javax.swing.BorderFactory.createTitledBorder("Plain Text"));

        txtPlain.setColumns(20);
        txtPlain.setRows(5);
        jScrollPane1.setViewportView(txtPlain);

        btnEncrypt.setText("Encrypt");
        btnEncrypt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEncryptActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnPlainLayout = new javax.swing.GroupLayout(pnPlain);
        pnPlain.setLayout(pnPlainLayout);
        pnPlainLayout.setHorizontalGroup(
            pnPlainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE)
            .addGroup(pnPlainLayout.createSequentialGroup()
                .addGap(157, 157, 157)
                .addComponent(btnEncrypt)
                .addContainerGap(172, Short.MAX_VALUE))
        );
        pnPlainLayout.setVerticalGroup(
            pnPlainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnPlainLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnEncrypt))
        );

        add(pnPlain);

        pnEncrypted.setBorder(javax.swing.BorderFactory.createTitledBorder("Encrypted Text"));

        txtEncrypted.setColumns(20);
        txtEncrypted.setRows(5);
        jScrollPane2.setViewportView(txtEncrypted);

        btnDecrypt.setText("Decrypt");
        btnDecrypt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDecryptActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnEncryptedLayout = new javax.swing.GroupLayout(pnEncrypted);
        pnEncrypted.setLayout(pnEncryptedLayout);
        pnEncryptedLayout.setHorizontalGroup(
            pnEncryptedLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnEncryptedLayout.createSequentialGroup()
                .addGap(141, 141, 141)
                .addComponent(btnDecrypt)
                .addContainerGap(186, Short.MAX_VALUE))
            .addGroup(pnEncryptedLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnEncryptedLayout.createSequentialGroup()
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        pnEncryptedLayout.setVerticalGroup(
            pnEncryptedLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnEncryptedLayout.createSequentialGroup()
                .addContainerGap(102, Short.MAX_VALUE)
                .addComponent(btnDecrypt))
            .addGroup(pnEncryptedLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnEncryptedLayout.createSequentialGroup()
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(30, Short.MAX_VALUE)))
        );

        add(pnEncrypted);

        pnPlainResult.setBorder(javax.swing.BorderFactory.createTitledBorder("Result Plain Text"));

        txtResult.setColumns(20);
        txtResult.setEditable(false);
        txtResult.setRows(5);
        jScrollPane3.setViewportView(txtResult);

        javax.swing.GroupLayout pnPlainResultLayout = new javax.swing.GroupLayout(pnPlainResult);
        pnPlainResult.setLayout(pnPlainResultLayout);
        pnPlainResultLayout.setHorizontalGroup(
            pnPlainResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE)
        );
        pnPlainResultLayout.setVerticalGroup(
            pnPlainResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
        );

        add(pnPlainResult);
    }// </editor-fold>//GEN-END:initComponents

    private void btnEncryptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEncryptActionPerformed
        try {
            // Initialize the cipher with secret key to encrypt data
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            // Read the data into byte array
            byte[] text = txtPlain.getText().getBytes();
            // Store the encrypted data in a byte array
            byte[] encryptedData = cipher.doFinal(text);
            // Display the encrypted text in the textarea

            String encryptedText = Base64.encode(encryptedData);
            txtEncrypted.setText(encryptedText);
        } catch (Exception ex) {
            Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnEncryptActionPerformed

    private void btnDecryptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDecryptActionPerformed
        try {
            // Initialize the cipher with secret key to decrypt data
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            // Read the encrypted data into byte array
            byte[] decryptedText = Base64.decode(txtEncrypted.getText().getBytes());
            // Store the decrypted data into byte array
            byte[] plainData = cipher.doFinal(decryptedText);
            // Display the decrypted data in the textarea
            String plainText = new String(plainData);
            txtResult.setText(plainText);
        } catch (Exception ex) {
            Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnDecryptActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDecrypt;
    private javax.swing.JButton btnEncrypt;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JPanel pnEncrypted;
    private javax.swing.JPanel pnPlain;
    private javax.swing.JPanel pnPlainResult;
    private javax.swing.JTextArea txtEncrypted;
    private javax.swing.JTextArea txtPlain;
    private javax.swing.JTextArea txtResult;
    // End of variables declaration//GEN-END:variables
}
